package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Component
@Slf4j
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String msg) {
//        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "error.queue",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "error.direct"
            ),
            key = {"error"}
    ))
    public void errorQueue(String msg) {
        log.debug("处理失败的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        log.info("消费者接收到simple.queue的消息：【{}】", msg);
        // 模拟异常
        System.out.println(1 / 0);
        log.debug("消息处理完成！");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "dl.ttl.queue"
            ),
            exchange = @Exchange(
                    name = "dl.ttl.exchange"
            ),
            key = "dl"
    ))
    public void listenDlQueue(String msg) {
        log.info("接收到 ttl.queue的延迟消息：{}", msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "delay.queue",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "delay.exchange",
                    delayed = "true"
            ),
            key = "delay"
    ))
    public void listenDelayQueue(String msg) {
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }

}
